/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tableprocessor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class PriceDAO
{

    private  DefaultMutableTreeNode root  = new DefaultMutableTreeNode("Категории");
    private List<Pare> clist;
    private List<ColumnPare> colList = new ArrayList<ColumnPare>();
    private Comparator<Pare> comp = new Comparator <Pare>()

    {
        public int compare (Pare obj1, Pare obj2)
        {
            if (obj1.getDbcat().getId() > obj2.getDbcat().getId())
            return 1;
            else return -1;
        }
    };
    Iterator<Pare> getCathegories()
    {
        return clist.iterator();
    }
    Iterator<ColumnPare> getColumns()
    {
        return colList.iterator();
    }
    Iterator<Pare> getChildsForCathegory(Pare p)
    {
        List<Pare> list = new ArrayList<Pare>();
        for (int i = 0 ; i < clist.size(); i++)
        {
            if (clist.get(i).getDbcat().getParent() == p.getDbcat().getId())
            {
                list.add(clist.get(i));
            }
        }
        return list.iterator();
    }
    public JTree getTree()
    {
        
        root = new DefaultMutableTreeNode("Категории");
        fillTree();
        JTree tree = new JTree(root);
        CathegoryStaticTreeFactory.setPriceDAO(this);
        return tree;
    }
    public  void RenameCathegory(DefaultMutableTreeNode node, String name)
    {
        Pare p = findByNode(node);
        if (!this.root.equals(p))
        {
            DBCathegoryDAO.renameCathegory(name, p.getDbcat().getId(), p.getDbcat().getParent());
        }
    }
    public void addCathegory(DefaultMutableTreeNode rootn, String name)
    {
        Pare p = findByNode(rootn);
        {
            if (p!=null)
            {
                if (root.equals(p.getNode()))
                {
                    Pare np = new Pare(new DBCathegory(name,0),
                        new DefaultMutableTreeNode(name));
                    DBCathegoryDAO.addCathegory(np.getDbcat().getName(), np.getDbcat().getParent());
                    root.add(np.getNode());
                    clist.add(np);

                }
                else
                {
                    Pare np = new Pare(new DBCathegory(name,p.getDbcat().getId()),
                        new DefaultMutableTreeNode(name));
                    DBCathegoryDAO.addCathegory(np.getDbcat().getName(), np.getDbcat().getParent());
                    clist.add(np);
                    p.getNode().add(np.getNode());
                }
                root = new DefaultMutableTreeNode("Категории");
                fillTree();

                
            }
        }

    }
    public void addColumn(DefaultMutableTreeNode rootn, String name)
    {
        Pare p = findByNode(rootn);
        if (p!=null && !root.equals(p.getNode()))
        {
            if (!isHasChild(p.getDbcat().getId()))
            {

                   ColumnPare cp = new ColumnPare(new DBColumn(p.getDbcat().getId(),0,name),p);
                   DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
                   cp.setNode(node);
                   p.getNode().add(node);
                   colList.add(cp);
                   DBColumnDAO.addColumn(cp.getDbcol().getCathegory_id(),
                           cp.getDbcol().getName(),cp.getDbcol().getNumber() );
            }
        }

    }
    public DBColumn findColumnByNode(DefaultMutableTreeNode node)
    {
        for (int i = 0 ; i < colList.size(); i++)
        {
            if (colList.get(i).getNode().equals(node))
            {
                return colList.get(i).getDbcol();
            }
        }
        return null;
    }
    public void removeColumn(DefaultMutableTreeNode node)
    {
        for (int i = 0 ; i < colList.size(); i++)
        {
            if (colList.get(i).getNode().equals(node))
            {
                DBColumnDAO.removeColumn(colList.get(i).getDbcol().getId());
            }
        }
    }
    public void renameColumn(DefaultMutableTreeNode node, String name)
    {
         for (int i = 0 ; i < colList.size(); i++)
         {
             if (colList.get(i).getNode().equals(node))
             {
                 DBColumnDAO.renameColumn(name,colList.get(i).getDbcol().getCathegory_id() ,
                         colList.get(i).getDbcol().getId());

             }
         }
    }
    public boolean isHasChild(int id)
    {
        for (int i = 0 ; i < clist.size(); i++)
        {
            if (clist.get(i).getDbcat().getParent() == id)
            {
                return true;
            }
        }
        return false;
    }
    public boolean isHasChild(DefaultMutableTreeNode node)
    {
        Pare p = findByNode(node);
        if (p!=null)
        {
            return isHasChild(p.getDbcat().getId());
        }
        else
        {
            return false;
        }

    }
    public boolean isColumn(DefaultMutableTreeNode node)
    {
        for(int i = 0 ; i < colList.size(); i++)
        {
            if (colList.get(i).getNode().equals(node))
            {
                return true;
            }
        }
        return false;
    }
    public boolean removeCathegory(DefaultMutableTreeNode node)
    {
        if (!isHasChild(node))
        {
            if (findByNode(node) != null)
            {
                if (isHasColumns(node))
                {
                    DBColumnDAO.removeColumnByCathegoryId(findByNode(node).getDbcat().getId());
                }
                DBCathegoryDAO.removeCathegory(findByNode(node).getDbcat().getId());
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    public boolean isHasColumns(DefaultMutableTreeNode node)
    {
        for (int i = 0 ; i < colList.size(); i++)
        {
            if (colList.get(i).getPare().getNode().equals(node))
            {
                return true;
            }
        }
        return false;
    }
    public  Pare findByNode(DefaultMutableTreeNode node)
    {
        if (this.root.equals(node))
        {
            return new Pare(null,this.root);
        }
        for (int i = 0; i < clist.size(); i++)
        {
            if (clist.get(i).getNode().equals(node))
            {
                return clist.get(i);
            }
        }
        return null;
    }
    public void fillTree()
    {
        loadCathegories();
         for (int i = 0 ; i < clist.size(); i++)
         {
             if (clist.get(i).getDbcat().getParent() == 0)
             {
                 clist.get(i).setNode(new DefaultMutableTreeNode(clist.get(i).getDbcat().getName()));
                 root.add(clist.get(i).getNode());
             }
            else
            {
                 clist.get(i).setNode(new DefaultMutableTreeNode(clist.get(i).getDbcat().getName()));
                 getRoot(clist.get(i)).getNode().add(clist.get(i).getNode());
            }
        }
        loadColumns();
        CathegoryStaticTreeFactory.setPriceDAO(this);
       // loadProduce();
    }
    public void fillCathegoryStaticTree(CathegoryStaticTree _tree)
    {
        _tree.fill(clist, colList, root);
    }
   public  Iterator<ColumnPare> getColumnsForCathegory(DefaultMutableTreeNode node)
   {
       List<ColumnPare> ctmpList = new ArrayList<ColumnPare>();
       for (int i = 0 ; i < colList.size(); i++)
       {
           if (colList.get(i).getPare().getNode().equals(node))
           {
               ctmpList.add(colList.get(i));
           }
       }
       return ctmpList.iterator();
   }
    private Pare getRoot(Pare p)
    {
        for (int i = 0 ; i < clist.size(); i++)
        {
            if (clist.get(i).getDbcat().getId() == p.getDbcat().getParent())
            {
                return clist.get(i);
            }
        }
        return null;
    }
    public int getRowsInCathegory(DefaultMutableTreeNode node)
    {
        int size = 0;
        Pare p = this.findByNode(node);
        if (p!=null)
        {
            return DBProduceDAO.getRowCountForCathegory(p.getDbcat().getId());
        }
        return size;
    }
    public Iterator<String> getRowByCathegory(DefaultMutableTreeNode node, int row)
    {
        List<String> list = new ArrayList<String>();
        Iterator<ColumnPare> citer = this.getColumnsForCathegory(node);
        while(citer.hasNext())
        {
            int id =  this.findByNode(node).getDbcat().getId();
            String str = DBProduceDAO.getProduceByCathegoryColumnRow(id,
                    citer.next().getDbcol().getId(), row).getValue();
            if (str!=null)
            list.add(str);
        }
        return list.iterator();
    }
    public void clearProduceTable()
    {
        DBProduceDAO.clear();
    }
    public void clearProduceTableByCathegory(DefaultMutableTreeNode node)
    {
        DBProduceDAO.clearByCathegoryId(this.findByNode(node).getDbcat().getId());
    }
    public void loadColumns()
    {
        Iterator<DBColumn> iter;
        iter = DBColumnDAO.getColumns();
       
        while (iter.hasNext())
        {
            DBColumn col = iter.next();
            for(int i = 0 ; i < clist.size(); i++)
            {
                if (clist.get(i).getDbcat().getId()==col.getCathegory_id())
                {
                    ColumnPare cp = new ColumnPare(col,clist.get(i));
                    DefaultMutableTreeNode node = new DefaultMutableTreeNode("["+col.getName()+"]");
                    cp.setNode(node);
                    clist.get(i).getNode().add(cp.getNode());
                    colList.add(cp);
                }
            }
        }
    }
    private void loadProduce()
    {
//       Iterator <Integer> it =  DBProduceDAO.getCathegoryList();
//       while (it.hasNext())
//       {
//           Integer n = it.next();
//           for (int i = 0 ; i < clist.size(); i++)
//           {
//              if (clist.get(i).getDbcat().getId() == n)
//              {
//                  Iterator<DBProduce> pit = DBProduceDAO.getProduceByCathegory(n);
//                  while (pit.hasNext())
//                  clist.get(i).getNode().add(new DefaultMutableTreeNode(pit.next().getName()));
//              }
//           }
//       }
    }
    public void loadCathegories()
    {
       Iterator <DBCathegory> iter = DBCathegoryDAO.getDBCathegoryList();
       if (iter != null)
       {
            clist = new ArrayList<Pare>();
            while (iter.hasNext())
            {
                clist.add(new Pare(iter.next(),null));
            }
            Collections.sort(clist,comp);
       }
    }
}
//class Pare
//{
//    private DBCathegory dbcat = null;
//    private DefaultMutableTreeNode node = null;
//    public Pare(DBCathegory _cat,DefaultMutableTreeNode _node )
//    {
//        dbcat = _cat;
//        node = _node;
//    }
//    public void setDbcat(DBCathegory _cat)
//    {
//        dbcat = _cat;
//    }
//    public void setNode(DefaultMutableTreeNode _node)
//    {
//        node = _node;
//    }
//    public DBCathegory getDbcat()
//    {
//        return dbcat;
//    }
//    public DefaultMutableTreeNode getNode()
//    {
//        return node;
//    }
//}
//class ColumnPare
//{
//    private DBColumn dbcol = null;
//    private Pare parentCathegory = null;
//    private DefaultMutableTreeNode node = null;
//    public ColumnPare(DBColumn _dbcol,Pare _parentCathegory)
//    {
//        dbcol = _dbcol;
//        parentCathegory = _parentCathegory;
//    }
//    public DefaultMutableTreeNode getNode()
//    {
//        return node;
//    }
//    public void setNode(DefaultMutableTreeNode _node)
//    {
//        node = _node;
//    }
//    public void setDbcol(DBColumn _dbcol)
//    {
//        dbcol = _dbcol;
//    }
//    public DBColumn getDbcol()
//    {
//        return dbcol;
//    }
//    public void setPare(Pare _parentCathegory)
//    {
//        parentCathegory = _parentCathegory;
//    }
//    public Pare getPare()
//    {
//        return parentCathegory;
//    }
//}
